package template;

import containers.Generator;
import template.coffee.Coffee;
import template.coffee.Latte;
import template.coffee.Mocha;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

interface Addable<T> { void add(T t);}

public class Fill2 {
    public static <T> void fill(Addable<T> addable,Class<? extends T> classToken,int size){
        for (int i =0;i < size;i ++){
            try{
                addable.add(classToken.newInstance());
            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    }

    public static <T> void fill(Addable<T> addable, Generator<T> generator, int size){
        for (int i = 0;i < size;i ++){
            addable.add(generator.next());
        }
    }
}

class AddableCollectionAdapter<T> implements Addable<T>{
    private Collection<T> c;
    public AddableCollectionAdapter(Collection<T> c){
        this.c = c;
    }

    @Override
    public void add(T t) {
        c.add(t);
    }
}

class Adapter{
    public static <T> Addable<T> CollectionAdapter(Collection<T> c){
        return new AddableCollectionAdapter<T>(c);
    }
}

class AddableSimpleQueue<T> extends SimpleQueue<T> implements Addable<T> {
    @Override
    public void add(T t) {
        super.add(t);
    }
}

class Fill2Test{
    public static void main(String[] args){
        List<Coffee> carrier = new ArrayList<Coffee>();
        Fill2.fill(new AddableCollectionAdapter<Coffee>(carrier),Coffee.class,3);
        Fill2.fill(Adapter.CollectionAdapter(carrier),Latte.class,3);
        for (Coffee c : carrier){
            System.out.println(c);
        }

        AddableSimpleQueue<Coffee> coffeequeue = new AddableSimpleQueue<Coffee>();
        Fill2.fill(coffeequeue,Mocha.class,4);
        Fill2.fill(coffeequeue,Latte.class,1);
        for (Coffee c: coffeequeue){
            System.out.println(c);
        }
    }
}